package response

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

// 错误码定义
const (
	SUCCESS       = 200
	ERROR         = 500
	InvalidParams = 400
	UNAUTHORIZED  = 401
	FORBIDDEN     = 403
	NotFound      = 404
)

type Response struct {
	Code int    `json:"code"` //状态码
	Data any    `json:"data"` //数据
	Msg  string `json:"msg"`  //消息
}

var MsgFlags = map[int]string{
	SUCCESS:       "成功",
	ERROR:         "失败",
	InvalidParams: "请求参数错误",
	UNAUTHORIZED:  "未授权",
	FORBIDDEN:     "禁止访问",
	NotFound:      "资源不存在",
}

func GetMsg(code int) string {
	msg, ok := MsgFlags[code]
	if ok {
		return msg
	}
	return MsgFlags[ERROR]
}

func OkWithData(c *gin.Context, code int, msg string, data any) {
	c.JSON(http.StatusOK, Response{
		Code: code,
		Data: data,
		Msg:  msg,
	})
}

func Ok(c *gin.Context, code int, msg string) {
	c.JSON(http.StatusOK, Response{
		Code: code,
		Data: map[string]interface{}{},
		Msg:  msg,
	})
}

func Error(c *gin.Context, code int, msg string) {
	c.JSON(http.StatusOK, Response{
		Code: code,
		Data: map[string]interface{}{},
		Msg:  msg,
	})
}
